/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.enums.BooleanTypeEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.shop.api.entity.GoodsBanner;
import com.pig4cloud.pigx.shop.api.entity.GoodsShareConfig;
import com.pig4cloud.pigx.shop.service.GoodsMaterialService;
import com.pig4cloud.pigx.shop.service.GoodsShareConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序分享配置
 *
 * @author pigx code generator
 * @date 2024-01-11 15:42:18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/goods/share/config")
@Tag(description = "goods_share_config", name = "小程序分享配置管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GoodsShareConfigController {

	private final GoodsShareConfigService goodsShareConfigService;

	private final GoodsMaterialService materialService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param goodsShareConfig 小程序分享配置
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getGoodsShareConfigPage(Page page, GoodsShareConfig goodsShareConfig) {
		LambdaQueryWrapper<GoodsShareConfig> lqw = new LambdaQueryWrapper<>();
		if (!StringUtils.isEmpty(goodsShareConfig.getShareTitle())) {
			lqw.like(GoodsShareConfig::getShareTitle, goodsShareConfig.getShareTitle());
		}
		lqw.orderByDesc(GoodsShareConfig::getCreateTime);
		IPage<GoodsShareConfig> items = goodsShareConfigService.page(page, lqw);
		items.getRecords().forEach(item -> {
			if (!StringUtils.isEmpty(item.getShareImage())) {
				item.setShareImage(materialService.convertImageIdToUrl(item.getShareImage()));
			}
		});

		return R.ok(items);
	}

	/**
	 * 通过id查询小程序分享配置
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		GoodsShareConfig goodsShareConfig = goodsShareConfigService.getById(id);
		if (!StringUtils.isEmpty(goodsShareConfig.getShareImage())) {
			goodsShareConfig.setShareImage(materialService.convertImageIdToUrl(goodsShareConfig.getShareImage()));
		}
		return R.ok(goodsShareConfig);
	}

	/**
	 * 新增小程序分享配置
	 * @param goodsShareConfig 小程序分享配置
	 * @return R
	 */
	@Operation(summary = "新增小程序分享配置", description = "新增小程序分享配置")
	@SysLog("新增小程序分享配置")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('goods_share_config_add')")
	public R save(@RequestBody GoodsShareConfig goodsShareConfig) {
		return R.ok(goodsShareConfigService.save(goodsShareConfig));
	}

	/**
	 * 修改小程序分享配置
	 * @param goodsShareConfig 小程序分享配置
	 * @return R
	 */
	@Operation(summary = "修改小程序分享配置", description = "修改小程序分享配置")
	@SysLog("修改小程序分享配置")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('goods_share_config_edit')")
	public R updateById(@RequestBody GoodsShareConfig goodsShareConfig) {
		return R.ok(goodsShareConfigService.updateById(goodsShareConfig));
	}

	/**
	 * 通过id删除小程序分享配置
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除小程序分享配置", description = "通过id删除小程序分享配置")
	@SysLog("通过id删除小程序分享配置")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('goods_share_config_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(goodsShareConfigService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param goodsShareConfig 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('goods_share_config_export')")
	public List<GoodsShareConfig> export(GoodsShareConfig goodsShareConfig) {
		return goodsShareConfigService.list(Wrappers.query(goodsShareConfig));
	}

	/**
	 * 获取已上架配置项
	 * @return
	 */
	@Inner(false)
	@GetMapping("/list")
	public R getGoodsShareConfigList() {
		return R.ok(goodsShareConfigService.list(Wrappers.<GoodsShareConfig>lambdaQuery()
				.eq(GoodsShareConfig::getStatus, BooleanTypeEnum.TRUE.getType())));
	}

}
